package com.yuan.spring.fastjson;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 袁恩光 on 2017/11/20.
 */
public class JSONTest {


    private static final Logger logger = LoggerFactory.getLogger(JSONTest.class);

    @Test
    public void tt() {
        JSONBean jsonBean = new JSONBean();
        jsonBean.setName("袁恩光");
        logger.info("转换成jsonString");
        String jsonStr1 = JSONObject.toJSONString(jsonBean);
        System.out.println(jsonStr1);
        logger.info("从str转化成对象");
        jsonBean = JSONObject.parseObject(jsonStr1, JSONBean.class);
        System.out.println(jsonBean.getName());
        logger.info("转换成jsonString带list");
        ListBean listBean = new ListBean();
        listBean.setGender("男");
        listBean.setId("18");
        List<ListBean> listBeanList = new ArrayList<>();
        listBeanList.add(listBean);
        jsonBean.setListBeanList(listBeanList);
        logger.info("转换成jsonString");
        String jsonStr2 = JSONObject.toJSONString(jsonBean);
        System.out.println(jsonStr2);
        logger.info("从str转化成对象");
        jsonBean = JSONObject.parseObject(jsonStr2, JSONBean.class);
        System.out.println(jsonBean.getListBeanList().get(0).getGender());
        System.out.println(JSONObject.toJSONString(listBeanList, SerializerFeature.DisableCircularReferenceDetect,SerializerFeature.PrettyFormat));

    }

}
